//
//  RxTableTest.swift
//  CooperSwift_Example
//
//  Created by 计海峰 on 2021/4/29.
//  Copyright © 2021 CocoaPods. All rights reserved.
//

import Foundation
import UIKit
import RxSwift
import RxDataSources
import Then
import CooperSwift


struct TestSection {
    var language: String
    var items: [Item]
}

extension TestSection : SectionModelType {
    
    typealias Item = String
    
    init(original: TestSection, items: [String]) {
        self = original
        self.items = items
    }
    
}


class RxDataSourceTableVC: UIViewController {
    
    let disposeBag = DisposeBag()
    
    var dataSource : RxTableViewSectionedReloadDataSource<TestSection>?
    
    
    private let tv: UITableView = {
        var tv = UITableView.init()
        tv.backgroundColor = UIColor.white
        return tv
    }()
    
    private let btn: UIButton = {
        var btn = UIButton.init()
        btn.setTitle("刷新", for: .normal)
        btn.backgroundColor = UIColor.random()
        return btn
    }()
    
   
    
    override func viewDidLoad() {
        super.viewDidLoad()
        view.addSubviews([tv, btn])
        
        tv.snp.makeConstraints { (make) in
            make.edges.equalTo(view).inset(UIEdgeInsets.zero)
        }
        
        btn.snp.makeConstraints { (make) in
            make.right.bottom.equalTo(view).offset(-44)
            make.width.height.equalTo(44)
        }
        
        dataSource = RxTableViewSectionedReloadDataSource<TestSection>(
            configureCell: { (datasource, tv, indexPath, element) -> UITableViewCell in
                let cell: RxTableTestCell = tv.dequeueReusableCell(forIndexPath: indexPath)
                cell.titleLB.text = "indexPath -> \(indexPath.row) element -> \(element)"
                return cell
            }
//            titleForFooterInSection: { (datasource, index) -> String? in
//                return datasource[index].language
//            },
        )
        
        
//        let items = [TestSection(language: "language", items: ["Objective-C", "Swift", "RXSwift"])]
//        Observable.just(items).bind(to: tv.rx.items(dataSource: dataSource!)).disposed(by: disposeBag)
        
        // flatMap
        let randomResult = btn.rx.tap.asObservable().startWith(()).flatMapLatest(getRandomResult).share(replay: 1)

//        //随机的表格数据
//        let randomResult = btn.rx.tap.asObservable()
//            .startWith(()) //加这个为了让一开始就能自动请求一次数据
//            .flatMapFirst(getRandomResult)  //连续请求时只取第一次数据
//            .share(replay: 1)
        
//        //随机的表格数据
//        let randomResult = btn.rx.tap.asObservable()
//            .throttle(1, scheduler: MainScheduler.instance) //在主线程中操作，1秒内值若多次改变，取最后一次
//            .startWith(()) //加这个为了让一开始就能自动请求一次数据
//            .flatMapLatest(getRandomResult)
//            .share(replay: 1)
        
        randomResult.bind(to: tv.rx.items(dataSource: dataSource!)).disposed(by: disposeBag)
    }
    
    //获取随机数据
    func getRandomResult() -> Observable<[TestSection]> {
        print("正在请求数据......")
        let items = (0 ..< 5).map {_ in
            String(Int(arc4random()))
        }
        let observable = Observable.just([TestSection(language: "test", items: items)])
        return observable.delaySubscription(RxTimeInterval.seconds(1), scheduler: MainScheduler.instance)
    }
    
}
